package com.ky.tms.controller;


import com.ky.tms.entity.TmsLine;
import com.ky.tms.entity.TmsLinePrice;
import com.ky.tms.enums.RoleEnum;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.service.ITmsLinePriceService;
import com.ky.tms.service.ITmsLineService;
import com.ky.tms.util.CommonResult;
import com.ky.tms.util.RequestContextHolderUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chengkaimao
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/tms/tms-line")
public class TmsLineController {

    @Autowired
    private ITmsLineService tmsLineService;

    @Autowired
    private ITmsLinePriceService tmsLinePriceService;

    @ApiOperation("线路数据分页")
    @GetMapping("page")
    public CommonResult page(TmsLine tmsLine, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        return CommonResult.buildSuccess(tmsLineService.page(tmsLine, pageSize, current));
    }

    @ApiOperation("查询线路价格")
    @GetMapping("price-page")
    public CommonResult selectLinePrice(@RequestParam Long lineId){
        return CommonResult.buildSuccess(tmsLineService.selectLinePrice(lineId));
    }

    @ApiOperation("创建线路")
    @PostMapping
    public CommonResult add(@RequestBody TmsLine tmsLine) throws PredictableException {
        tmsLineService.add(tmsLine);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("修改线路")
    @PutMapping
    public CommonResult update(@RequestBody TmsLine tmsLine) throws PredictableException {
        tmsLineService.update(tmsLine);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("创建价格")
    @PostMapping("price")
    public CommonResult addPrice(@RequestBody TmsLinePrice tmsLinePrice) throws PredictableException {
        tmsLinePriceService.add(tmsLinePrice);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("修改价格")
    @PutMapping("price")
    public CommonResult updatePrice(@RequestBody TmsLinePrice tmsLinePrice) throws PredictableException {
        tmsLinePriceService.update(tmsLinePrice);
        return CommonResult.buildSuccess(null);
    }
    @ApiOperation("删除线路")
    @DeleteMapping
    public CommonResult delete(@RequestBody List<Long> ids){
        tmsLineService.delete(ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("搜索下拉数据")
    @GetMapping("select-list")
    public CommonResult selectList(@RequestParam Long customerId) {
        return CommonResult.buildSuccess(tmsLineService.selectList(customerId));
    }

    @ApiOperation("是否有权限编辑价格")
    @GetMapping("hasPrice")
    public CommonResult hasPrice() {
        String role = RequestContextHolderUtil.getRole();
        if(RoleEnum.SERVICE.getValue().equals(role)){
            return CommonResult.buildSuccess(false);
        }else{
            return CommonResult.buildSuccess(true);
        }

    }




}
